package com.sufun.base;

import java.io.File;

import android.app.Application;

import com.ab.http.AbHttpUtil;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.sufun.sfdroid.R;
/**
 * 基础的Application类
 * @author Wu
 *
 */
public class SFBaseApplication extends Application {
	
	
	private static SFBaseApplication app;
	/**
	 * 图片下载器
	 */
	public ImageLoader  imageLoader=null;
	/**
	 * 图片下载缓存处理器
	 */
	public  DisplayImageOptions options ;	
	/**
	 * http 请求工具
	 */
	public AbHttpUtil mAbHttpUtil = null;
	public static SFBaseApplication shareSFApplication()
	{
		return app;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		app=this;
		
        //获取Http工具类
        mAbHttpUtil = AbHttpUtil.getInstance(this);
        mAbHttpUtil.setTimeout(10000);
        //初始化图片下载器
        initImageLoader();
	}
	
	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
	}
	File cacheDir ;
	/**
	 * 图片下载器
	 */
	public void initImageLoader()
	{
		cacheDir = StorageUtils.getOwnCacheDirectory(this, "sufun/images");
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
        .memoryCacheExtraOptions(480, 800) // max width, max height
        .threadPoolSize(2)
        .threadPriority(Thread.NORM_PRIORITY - 3)
        .denyCacheImageMultipleSizesInMemory()
        .memoryCache(new UsingFreqLimitedMemoryCache(1 * 1024 * 1024)) // You can pass your own memory cache implementation
        .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
        .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
        .discCacheFileNameGenerator(new Md5FileNameGenerator())
        .tasksProcessingOrder(QueueProcessingType.LIFO)
        .build();
		
		//Initialize ImageLoader with configuration
		imageLoader=ImageLoader.getInstance();
		imageLoader.init(config);
		
	    options = new DisplayImageOptions.Builder()  
	        .showStubImage(R.drawable.d_500_default_image)  
	        .showImageForEmptyUri(R.drawable.image_no)  
	        .showImageOnFail(R.drawable.image_error)  
	        .cacheInMemory(true)  
	        .cacheOnDisc(true)
	        .build(); 
	}
}
